<?php

namespace Upsanddowns\Controller;
//use Think\Controller;

class RankingListController extends BaseController {
	
	// 猜涨跌首页
	public function index() {    
    //排列所有参与人员的排名
    $rankList = $this->getRankList();
    $this->assign('rankList',$rankList);
    //取出我的排名
    $myRank = $this->getMyRank($rankList);
    $this->assign('myRank',$myRank);
    $this->display();
	}

  /**
   * [getMyRank 获取我当前期的总排名]
   * @return [type] [description]
   */
  private function getMyRank($rankList){
    $user = cookie('user_auth');
    $myRank = array();
    foreach($rankList as $rank=>$info){
      if($info['user_id'] == $user['uid']){
        $myRank = $info;
        $myRank['rank'] = $rank+1;
        break;
      }
      continue;
    }
    return $myRank;
  }

  /**
   * [getRankList 获取当前期所有排名明细]
   * @return [type] [description]
   */
  private function getRankList(){
    //获取当前期信息
    //$periods = M('RoseActivity')->getField('max(periods) as periods');
    $periods = M('RoseActivity')->where('status=1')->order('periods_start_time desc')->getField('periods');
    //取出当前期数的所有活动
    //$activites = M('RoseActivity')->where('periods='.$periods)->getField('id',true);
    //获取排名列表
    $where = array();
    $where['periods'] = $periods;
    $where['profit_gold_bean'] = array('neq',0);
    //$where['rose_activity_id'] = array('in',$activites);
    //$persons = M('RoseBuy')->distinct(true)->where($persons_where)->getField('user_id',true);
    $ranklist = M('RoseBuy')->field("user_id,sum(profit_gold_bean) as bean,count(*) as bits,min(date) as min_date,max(date) as max_date,max(add_time) as max_time")->where($where)->group('user_id')->order('bean desc,bits desc,min_date asc,max_date desc,max_time asc')->select();
    return $ranklist;
  }
}



